<?php
	/**
	 * SEND EMAILS TO SUBSCRIBERS
	 */
	// EMAIL TO SUBCRIBER DAY BY DAY
	function cpx_email_daybyday($email_to, $arr_cp_ids, $st_id, $follow_id)
	{
		//$email_to = 'haiht241@gmail.com';
		add_filter('wp_mail_content_type', 'set_html_content_type');
        include_once ("email_templates/email_store.php");

		$st_name = get_post_field('post_title', $st_id);
		$title = "Today $st_name coupons";

		$email_content = cpx_get_email_store($arr_cp_ids, $st_id, $follow_id);
        wp_mail($email_to, $title, $email_content);
		remove_filter('wp_mail_content_type', 'set_html_content_type');
	}
	function set_html_content_type()
	{
		return 'text/html';
	}
	// GET TO DAY COUPONS
	function cpx_today_coupons_by_store($st_id, $d = '')
	{
		global $wpdb;
        if($d == '')
            $today = date('Y-m-d', time());
        else
            $today = $d;

		$qr = "
        SELECT ID FROM {$wpdb->posts} WHERE post_type = 'coupon' AND post_status = 'publish'
        AND DATE_FORMAT(post_date, '%Y-%m-%d') = '{$today}' AND ID IN
        (SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key='store_coupon_metadata' AND meta_value = {$st_id})
        ";
		$rs = $wpdb->get_results($qr);
		if (count($rs) > 0)
		{
			$arr = array();
			foreach ($rs as $r)
			{
				array_push($arr, $r->ID);
			}
			return $arr;
		}
		else
			return null;
	}
	// GET STORE HAVE COUPONS PUBLISHED TODAY
	function cpx_today_store_have_cp_published($d = '')
	{
		global $wpdb;
        if($d == '')
		  $today = date('Y-m-d', time());
        else
          $today = $d;

		$qr = "
        SELECT meta_value AS 'store_id' FROM {$wpdb->postmeta} WHERE meta_key = 'store_coupon_metadata' AND post_id IN
        ( SELECT ID FROM {$wpdb->posts} WHERE post_type = 'coupon' AND post_status = 'publish'
          AND DATE_FORMAT(post_date, '%Y-%m-%d') = '{$today}' ) GROUP BY meta_value;
        ";
		$rs = $wpdb->get_results($qr, ARRAY_N);
		return $rs;
	}
	// GET STORES BEING SUBSCRIBED
	function cpx_get_stores_subscribed()
	{
		global $wpdb;
		$qr = "
        SELECT VALUE FROM wp_follow GROUP BY VALUE
        ";
		$rs = $wpdb->get_results($qr, ARRAY_N);
		return $rs;
	}
	// GET EMAILS SUBCRIBED BY STORE
	function cpx_get_emails_subscribed_bystore($st_id)
	{
		global $wpdb;
		$qr = "SELECT user_email,follow_id FROM wp_follow WHERE VALUE = {$st_id}";
		$rs = $wpdb->get_results($qr);
		return $rs;
	}
	// ACTIVE ACTION SEND EMAIL TO SUBSCRIBER DAY BY DAY
	function cpx_doaction_sendemail_daybyday($d = '')
	{
		$arr_st_have_cps_published = cpx_today_store_have_cp_published($d);
		$arr_st_subscribed = cpx_get_stores_subscribed();
		//include ("email_templates/email_store.php");
        global $wpdb;

		foreach ($arr_st_have_cps_published as $store_id)
		{
			if (in_array($store_id, $arr_st_subscribed))
			{
				$arr_emails_following_this_store = cpx_get_emails_subscribed_bystore($store_id[0]);
				$cps_published_today = cpx_today_coupons_by_store($store_id[0], $d);
                $str_cp_ids = '';
                foreach ($cps_published_today as $i=>$c) {
                    $str_cp_ids.= $c;
                    if($i != count($cps_published_today) - 1)
                        $str_cp_ids .= ',';
                }

				foreach ($arr_emails_following_this_store as $e)
				{
					//cpx_email_daybyday($e->user_email, $cps_published_today, $store_id[0], $e->follow_id);
					//sleep(5);// seconds
                    $qr = "INSERT INTO wp_emails(em_to,cp_ids,st_id,follow_id,status) VALUES('{$e->user_email}','$str_cp_ids',{$store_id[0]},{$e->follow_id},0)";
                    $wpdb->query($qr);
				}
			}
		}
	}
    function em_get_email_tosend()
    {
        global $wpdb;
        $qr = "SELECT * FROM wp_emails WHERE STATUS != 1 LIMIT 0,5";
        $rs = $wpdb->get_results($qr);
        return $rs;
    }
